package spring.controller;

import org.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;

@Controller
@RequestMapping("/mdm/api/v1/devices/{deviceId}")
public class RegisterController {

    @RequestMapping(method = RequestMethod.PUT)
    @ResponseBody
    public String register(@PathVariable String deviceId, HttpServletRequest req, HttpServletResponse res) throws IOException {
        System.out.println("RegisterController");
        System.out.println("deviceId=" + deviceId);
        InputStream inputStream = req.getInputStream();
        String str = IOUtils.stream2String(inputStream, "utf-8");
        System.out.println("get data = " + str); // ok

//        JSONObject json = new JSONObject(str);
//        String str2 = json.toString();
//        System.out.println("str2 = " + str2); // ok

        // return "Register Succ";
        JSONObject json2 = new JSONObject();
        json2.put("result", 0);
        json2.put("desc", "ok");
        String detail = "{\"code\": \"2001\",\"desc\": \"审核中...\"}";
        json2.put("detail", detail);

        return json2.toString();
    }
}
